package com.headline.utils;

public class Result<T> {
    private Integer code;//状态码
    private String message;//返回信息
    private T data;//返回数据

    //无参构造
    public Result(){}

    //返回数据
    protected static <T> Result<T> build(T data){
        Result<T> result = new Result<>();
        if(data != null){
            result.setData(data);
        }
        return result;
    }

    //返回数据封装
    public static <T> Result<T> build(T body,Integer code, String message){
        Result<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    //根据枚举类型返回结果数据
    public static <T> Result<T> build(T body ,ResultCodeEnum resultCodeEnum){
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //操作成功方法
    public static <T> Result<T> OK(T data){
        Result<T> result = build(data);
        return build(data,ResultCodeEnum.SUCCESS);
    }
    //构造message和code构造方法
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }
    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }


    //getter和setter方法

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
